package secondBrush.array;

public class E01LeetCode704 {
    //左闭右闭
    public int search(int[] nums, int target) {
        if (target < nums[0] || target > nums[nums.length - 1]) {
            return -1;
        }

        int length = nums.length;
        int start = 0, end = length - 1;
        while (start <= end) {
            int middle = (end + start) >> 1;
            if (nums[middle] > target) {
                end = middle - 1;
            } else if (nums[middle] < target) {
                start = middle + 1;
            } else {
                return middle;
            }
        }
        return -1;
    }

    //左闭右开
    public static int search2(int[] nums, int target) {


        int length = nums.length;
        int start = 0, end = length ;
        while (start < end) {
            int middle = (end + start) >> 1;
            if (nums[middle] > target) {
                end = middle ;
            } else if (nums[middle] < target) {
                start = middle + 1;
            } else {
                return middle;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] nums = {-1,0,3,5,9,12};
        int target = 9;
        System.out.println(search2(nums, 9));
    }


}
